package com.photographysharing.util;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

public class StringUtil {

    public static Map<String,Object> stringToMap(String str){
//        str = str.substring(2, str.length()-1);
//        String[] strs = str.split(",");
//        Map<String,String> map = new HashMap<String, String>();
//        for (String string : strs) {
//            String key = string.split(":")[0];
//            String value = string.split(":")[1];
//            // 去掉头部空格
//            String key1 = key.trim();
//            String value1 = value.trim();
//            map.put(key1, value1);
//        }
        JSONObject jsonObject = JSONUtil.parseObj(str);
        return jsonObject;
    }

    public static Object stringToObject(String jsonString){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Object genericObject = objectMapper.readValue(jsonString, Object.class);
            return genericObject;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }

    public static String mapToString(Object object){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String json = objectMapper.writeValueAsString(object);
            return json;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
